package com.troll.wanandroid.design.fragment

import android.content.Context
import android.content.Intent
import android.widget.FrameLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.troll.base.BaseFragment
import com.troll.eth.base.ui.ViewState
import com.troll.eth.ex.cast
import com.troll.wanandroid.R
import com.troll.wanandroid.design.activity.LinkActivity
import com.troll.wanandroid.adapter.home.HomeAdapter
import com.troll.wanandroid.adapter.home.HomeHeadBannerAdapter
import com.troll.wanandroid.model.ArticleIntroduction
import com.troll.wanandroid.model.BannerBean
import com.troll.wanandroid.viewmodel.HomeViewModel
import com.youth.banner.Banner
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnBannerListener

/**
 *
 * @Author:   TangPeng
 * @Time:     2/2/21
 * @Remark:
 */
class HomeFragment : BaseFragment<HomeViewModel>() {

    private var rv: RecyclerView? = null
    private val headView by lazy { HeadView(requireContext()) }

    override val layoutId: Int
        get() = R.layout.frag_home

    override fun flowOfData() {
        rv = findViewById<RecyclerView>(R.id.home_rv)
        val homeAdapter = HomeAdapter()
        rv?.adapter = homeAdapter
        homeAdapter.setHeaderView(headView)
        rv?.layoutManager = LinearLayoutManager(requireContext())


        with(viewModel) {
            getArticle()
            getBanner()
            liveArticle.observe {
                viewState = ViewState.NORMAL
                homeAdapter.setNewData(it)
                homeAdapter.notifyDataSetChanged()
            }
            liveBanner.observe {
                viewState = ViewState.NORMAL
                headView.setAdapter(it)
            }
            liveError.observe {
                viewState = ViewState.ERROR
            }
        }


        homeAdapter.setOnItemClickListener { adapter, _, position ->
            adapter.getItem(position)?.cast<ArticleIntroduction> {
                val intent = Intent(requireContext(), LinkActivity::class.java)
                intent.putExtra("link", it.link)
                startActivity(intent)
            }
        }
    }

    override fun flowOfSetup() {

    }

    override fun onRetryClick() {
        viewModel.getArticle()
        viewModel.getBanner()
    }

    inner class HeadView(context: Context) : FrameLayout(context, null, 0),
        OnBannerListener<BannerBean> {
        private var banner: Banner<BannerBean, HomeHeadBannerAdapter>? = null
        private val bannerAdapter by lazy { HomeHeadBannerAdapter(null) }

        init {
            inflate(context, R.layout.layout_home_rv_head, this)
            banner = findViewById(R.id.home_layout_banner)
            banner.apply {
                this?.indicator = CircleIndicator(context)
                this?.setAdapter(bannerAdapter)
                this?.setOnBannerListener(this@HeadView)
            }
        }

        fun setAdapter(list: MutableList<BannerBean>) {
            bannerAdapter.setDatas(list)
            bannerAdapter.notifyDataSetChanged()
        }

        override fun OnBannerClick(data: BannerBean?, position: Int) {
            val intent = Intent(context, LinkActivity::class.java)
            intent.putExtra("link", data?.url)
            context.startActivity(intent)
        }
    }

}